<template>
  <view class="goods-item">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <!-- 在购物车页面才加上单选框，而商品列表页不需要加 -->
      <radio @click="changeRadioCheck()" v-show="showRadio" :checked="goods.goods_state" color="#C00000"></radio>
      <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-name">{{goods.goods_name}}</view>
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price">￥{{goods.goods_price}}</view>
        <!-- 数量改变 -->
        <uni-number-box v-show="showNumberBox" :value="goods.goods_count || 1" :min="1" @change="changeGoodsNum"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    // 定义 props 属性，用来接收外界传递到当前组件的数据
    props: {
      // 商品的信息对象
      goods: {
        type: Object,
        defaul: {},
      },
      // 是否显示单选框
      showRadio: {
        type: Boolean,
        default: false
      },
      // 是否显示数量改变组件
      showNumberBox:{
        type:Boolean,
        default:false
      }
    },
    data() {
      return {
        // 默认的空图片
        defaultPic: 'http://image4.suning.cn/uimg/b2c/newcatentries/0070089410-000000000633718031_1_400x400.jpg',
      }
    },
    methods: {
      // 改变当选框选中状态
      changeRadioCheck() {
        // 新的选中状态
        const checked_status = !this.goods.goods_state
        // 触发父组件的修改状态的自定义事件
        this.$emit('on-radio-change', {
          id: this.goods.goods_id,
          checked_status
        })
      },
      // 改变商品的购买数量
      changeGoodsNum(e) {
        const new_goods_num = e
        // 触发父组件的修改状态的自定义事件
        this.$emit('on-num-change', {
          id: this.goods.goods_id,
          new_goods_num
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    // 让 goods-item 项占满整个屏幕的宽度
    width: 750rpx;
    // 设置盒模型为 border-box
    box-sizing: border-box;
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;

    .goods-item-left {
      margin-right: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .goods-pic {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
      }

      .goods-info-box {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .goods-price {
          font-size: 16px;
          color: #c00000;
        }
      }
    }
  }
</style>
